package com.ybdz.scanner.base;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.ybdz.scanner.activity.LoginActivity;
import com.ybdz.scanner.receiver.ScannerInterface;
import com.ybdz.scanner.utils.UIUtils;


/**
 * @ClassName: BaseActivity
 * @author: zhao
 * @Description:    BaseActivity
 * @date 2017/12/11 11:00
 */
public class BaseActivity extends AppCompatActivity {


    private static ScannerInterface mScanner;
    private static IntentFilter mIntentFilter;
    private static final String RES_ACTION = "android.intent.action.SCANRESULT";
    private QMUITipDialog mShowDialog;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mShowDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("加载中")
                .create();
    }

    protected ScannerInterface initScanner(BroadcastReceiver receiver){
        if (mScanner == null){
            mScanner = new ScannerInterface(this);
        }
        mScanner.open();                        //扫描引擎上电，该接口请勿频繁调用，频繁关闭串口会导致程序卡死
        mScanner.setOutputMode(1);              //扫描结果输出模式，此处为广播输出模式
        mScanner.unlockScanKey();               //释放设备的扫描按键，释放后iScan无法控制扫描按键，用户可自定义扫描按键
        mScanner.enablePlayBeep(true);          //是否允许蜂鸣反馈
        mScanner.enableFailurePlayBeep(false);   //扫描失败蜂鸣反馈
        mScanner.enablePlayVibrate(true);       //震动开启与关闭
        mScanner.timeOutSet(2);           //设置扫描延时2秒
        mScanner.lightSet(false);         //开启/关闭左上角扫描指示灯
        mScanner.SetErrorBroadCast(true);//扫描错误广播
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(RES_ACTION);
        this.registerReceiver(receiver, mIntentFilter);
        return mScanner;
    }

    protected void finishScanner(BroadcastReceiver receiver){
        mScanner.scan_stop();
        this.unregisterReceiver(receiver);
        mScanner.continceScan(false);
        //mScanner.close();
    }


    /**
     * 请求成功的Dialog;
     */
    protected void showLoading() {
        mShowDialog.show();
    }
    protected void hideLoading() {
        mShowDialog.dismiss();
    }

    protected void showLoadinging() {
        final QMUITipDialog showDialog;
        showDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("加载中")
                .create();
        showDialog.show();
    }

    /**
     * 请求失败的Dialog;
     */
    protected void onFail(){
        final QMUITipDialog failDialog;
        failDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
                .setTipWord("请求失败")
                .create();
        failDialog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                failDialog.dismiss();
            }
        },1500 );
    }

    protected void onFail(String hint){
        final QMUITipDialog failDialog;
        failDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
                .setTipWord(hint)
                .create();
        failDialog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                failDialog.dismiss();
            }
        },1500 );
    }

    /**
     * 请求失败的Dialog;
     */
    protected void noLogin(){
        final QMUITipDialog failDialog;
        failDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
                .setTipWord("你未登录，请登录账号")
                .create();
        failDialog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                failDialog.dismiss();
            }
        },1500 );
    }

    /**
     * 请求超时的Dialog;
     */
    protected void onFailTimeOut() {
        final QMUITipDialog failDialog;
        failDialog = new QMUITipDialog.Builder(this)//创建tipdialog实例为其设置显示样式
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)//设置有加载框模式的dialog
                .setTipWord("请求超时")
                .create();
        failDialog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                failDialog.dismiss();
            }
        },1500 );
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}